/**
 *
 */
package org.windowkit.toolkit.widget.tree;

import java.awt.Point;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 * @author Eric Lundin
 *
 */
public class Tree extends JTree {

	/**
	 *
	 */
	private static final long serialVersionUID = -2958102755063266694L;

	/**
	 * Creates a new Tree.
	 *
	 * @param model
	 */
	public Tree(final TreeModel model) {
		super(model);
	}

	@Override
	public String convertValueToText(final Object value, final boolean selected,
			final boolean expanded, final boolean leaf, final int row, final boolean hasFocus) {
		// TODO Auto-generated method stub
		return ((BaseDefaultMutableTreeNode) value).getDisplayValue();
	}

	/**
	 * Gets the tree node at the specified point in the the tree.
	 *
	 * @param p
	 *            The coordinate point at which to find the node.
	 * @return The node which is displayed at the given point.
	 */
	public DefaultMutableTreeNode getTreeNode(final Point p) {
		TreePath path = this.getPathForLocation(p.x, p.y);
		if (path != null) {
			return (DefaultMutableTreeNode) path.getLastPathComponent();
		}
		else {
			return null;
		}
	}
}
